
import React, { useMemo, useEffect } from 'react';
import PrintTable from '../fd/printview/components/table/PrintTable';
import { historyTask } from './api';
import { useState } from 'react';

interface AuditHistoryProps {
  businessId?: string | null;
  title?: string | null;
  workFlowInstanceId?: string | null;
  tableProps?: Object;
  onLoad?: () => void;
}

export default ({ businessId, title, workFlowInstanceId, onLoad, tableProps }: AuditHistoryProps) => {
  const [dataSource, setDataSource] = useState([]);

  const query = useMemo(() => {
    const where: Record<string, any> = {};
    
    if (businessId) where.businessId = businessId;
    else where.WorkFlowInstanceId = workFlowInstanceId;

    return { where, order: [['createdAt', 'ASC']] };
  }, [businessId, workFlowInstanceId]);

  useEffect(() => {
    historyTask(query).then((res) => {
      setDataSource(res.rows);
      onLoad?.();
    });
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [query]);

  return <PrintTable title={title} initialValue={dataSource} tableProps={tableProps} />;
};
